// pages/Moments /Moments.js
var that;
var db=wx.cloud.database();
var _=db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    homeAdvertises: [
      {
           'imgSrc': '../../images/ic_home_advertise.png',
           'webUrl': ''
      },
      {
           'imgSrc': '../../images/ic_home_advertise.png',
           'webUrl': ''
      },
      {
           'imgSrc': '../../images/ic_home_advertise.png',
           'webUrl': ''
      }
 ],
    userInfo:null,
    list:[],
    showOperationPannelIndex:-1,
    currentCircleIndex:-1,
    showCommentAdd:false,
    commentContent:'',
    heightBottom:'',
    haveMoreData:false,
    page:0,
    pageCount:10

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    wx.getStorage({
      key:'userInfo',
      success(res){
        that.setData({
          userInfo:JSON.parse(res.data)
        })
      }
    }),
   that.getList();
    // for(var i=1;i<10;i++)
    // {
    //   var circleData={};
    //   circleData.nickName="朋友-"+i;
    //   circleData.content="朋友发布内容-"+i;
    //   circleData.time="2020-05-0"+i;

    //   var imageList=[];
    //   var loveList=[];
    //   var commentList=[];

    //   circleData.imageList=imageList;
    //   circleData.loveList=loveList;
    //   circleData.commentList=commentList;

    //   for(var j=1;j<i;j++)
    //   {
    //     imageList.push(j);
    //     var loveData={};
    //     loveData.nickName="点赞-"+j;

    //     loveList.push(loveData);

    //     var commentData={};
    //     commentData.nickName="兰陵王-"+j+":";
    //     commentData.content="评论内容-"+j+":";

    //     commentList.push(commentData);
    //   }
    //   this.data.list.push(circleData);
    // }
    // that.setData({
    //   list:that.data.list
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showOperationPannel(e){
    var index=e.currentTarget.dataset.index;
    if(that.data.showOperationPannelIndex==index){
      that.setData({
        showOperationPannelIndex:-1
      })
    }else{
      that.setData({
        showOperationPannelIndex:index
      })
    }
  },
  clickLove(e){
    var index=e.currentTarget.dataset.index;
    var circleData=that.data.list[index];
    var loveList=circleData.loveList;
    var isHaveLove=false;
    for(var i=0;i<loveList.length;i++){
      if(that.data.userInfo._openid==loveList[i]._openid){
        isHaveLove=true;
        loveList.splice(i,1);
        //取消赞
        var loveData={};
        loveData.nickName=that.data.userInfo.nickName;
        loveData._openid=that.data.userInfo._openid;
        loveList.push(loveData);
        db.collection('circle').doc(circleData._id).update({
          data:{
            loveList:[]
          }
        })
        wx.showToast({
          icon:'none',
          title: '点赞已取消刷新后再试',
        })
        circleData.isLove=false;
        break;
      }
    }
      if(!isHaveLove){
        loveList.push({
          nickName:that.data.userInfo.nickName,
         _openid: that.data.userInfo._openid
        });
        //点赞
        var loveData={};
        loveData._openid=that.data.userInfo._openid;
        loveList.push(loveData);
        db.collection('circle').doc(circleData._id).update({
          data:{
            loveList:_.push(loveData)
          }
        })
        circleData.isLove=true;
        that.setData({
          list:that.data.list,
          showOperationPannelIndex:-1
        })
      }
    },
  bindInput(e){
    that.setData({
      commentContent:e.detail.value
    })
  },
  bindFocus(e){
    that.setData({
      heightBottom:e.detail.height
    })
  },
  clickComment(e){
    that.setData({
      currentCircleIndex:e.currentTarget.dataset.index,
      showCommentAdd:true,
      showOperationPannelIndex:-1,
    })
  },
  clickSend(e){
     var circleData =that.data.list[that.data.currentCircleIndex];
     var commentList=circleData.commentList;

     var commentData={};
     commentData.nickName=that.data.userInfo.nickName+":";
     commentData.content=that.data.commentContent;
     commentData._openid=that.data.userInfo._openid;
     commentList.push(commentData);

     that.setData({
       list:that.data.list,
       showCommentAdd:false,
       commentContent:''
     })
     db.collection('circle').doc(circleData._id).update({
       data:{
         commentList:_.push(commentData)
       }
     })

  },
    getList(){
      var currentPage=that.data.page;
      currentPage=0;
      db.collection('circle')
      .orderBy('time','desc')
      .skip(currentPage*that.data.pageCount)
      .limit(that.data.pageCount)
      .get()
      .then(res=>{
        that.setData({
          list:[]
        })
        if(res.data.length>0){
          for(var i=0;i<res.data.length;i++){
            res.data[i].time=that.js_date_time(res.data[i].time);
            that.data.list.push(res.data[i])
          }
          that.setData({
            list:that.data.list
          })
          if(res.data.length==that.data,pageCount){
            that.setData({
              haveMoreData:true
            })
          }else{
            that.setData({
              haveMoreData:false
            })
          }
        }
      })
      .catch(err=>{

      })
    },
    js_date_time(unixtime){
      var date=new Date(unixtime);
      var y=date.getFullYear();
      var m=date.getMonth()+1;
      m=m <10 ? ('0'+m) : m;
      var d=date.getDate();
      d=d <10 ? ('0'+d) : d;
      var h=date.getHours();
      h=h <10 ? ('0'+h) : h;
      var minute=date.getMinutes();
      var second=date.getSeconds();
      minute=minute <10 ? ('0'+minute) : minute;
      second= second <10 ? ('0'+ second) : second;
      return y+'-'+m+'-'+d+''+h+':'+minute+':'+second;
    }
})
